package com.mini.gateway.core.modules.server.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.mini.gateway.core.modules.route.entity.MiniRoute;
import com.mini.gateway.core.modules.route.service.RouteService;
import com.mini.gateway.core.modules.server.dto.ServerRelation;
import com.mini.gateway.web.domain.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ServerRelationService
 *
 */
@Slf4j
@Component
public class ServerRelationService {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RouteService routeService;
    @Autowired
    private DiscoveryClient discoveryClient;

    public List<ServerRelation> getList(String routeId) {
        List<ServerRelation> resultList = new ArrayList<>();
        try {
            MiniRoute route = routeService.getRoute(routeId);
            String uri = route.getUri();
            if (uri.startsWith("lb://")) {
                List<ServiceInstance> instanceList = discoveryClient.getInstances(route.getName());
                if (CollectionUtils.isNotEmpty(instanceList)) {
                    ServiceInstance instance = instanceList.get(0);
                    uri = instance.getUri().toString();
                }
            }
            if (StringUtils.isNotEmpty(uri)) {
                uri = uri + "/api/public/common/service/relation";
                JsonResult<Map<String, List<String>>> result = restTemplate.getForObject(uri, JsonResult.class);
                if (result.isSuccess()) {
                    Map<String, List<String>> dataMap = result.getData();
                    Set<String> keys = result.getData().keySet();
                    for (String key : keys) {
                        List<String> interfaceList = dataMap.get(key);
                        for (String ifName : interfaceList) {
                            ServerRelation relation = new ServerRelation(routeId, key, ifName);
                            resultList.add(relation);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            log.error("获取服务引用关系异常：", ex);
        }
        return resultList;
    }

    /*public Map<String, List<String>> getRelation(String packageName) {
        Map<String, List<String>> dataMap = new HashMap<>();
        Reflections reflections = new Reflections(packageName);
        Set<Class<?>> classList = reflections.getTypesAnnotatedWith(FeignClient.class);
        for (Class clazz : classList) {
            FeignClient fc = (FeignClient) clazz.getAnnotation(FeignClient.class);
            List<String> urlList = dataMap.get(fc.name());
            if (urlList == null) {
                urlList = new ArrayList<>();
                dataMap.put(fc.name(), urlList);
            }
            Method[] methods = clazz.getMethods();
            for (Method md : methods) {
                if (md.isAnnotationPresent(RequestMapping.class)) {
                    RequestMapping rm = md.getAnnotation(RequestMapping.class);
                    urlList.addAll(Arrays.asList(rm.value()));
                }
            }
        }
        return dataMap;
    }*/

}
